﻿#region Đào Mạnh Hòa
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace GUI.LoaiTheVip
{
    public partial class frmQuanLy : Form
    {
        #region Cap nhap giao diện main
        public delegate void GetBoolean(Boolean capnhat);
        public GetBoolean GetData;
        #endregion

        #region Delegate form xử lý
        public void GetValue(Boolean capnhat)
        {
            HienThi();
            if (!capnhat)
                Them();
        }

        void HienThi()
        {
            hienthi = Common.StaticVariable.InterfaceObject.SelectLoaiTheVip("1");
            dgvHienThi.DataSource = hienthi;
            fix();
        }

        void fix()
        {
            for (int i = 0; i < dgvHienThi.ColumnCount; i++)
            {
                dgvHienThi.Columns[i].Visible = false;
            }
            dgvHienThi.ReadOnly = true;
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.GhiChu].Visible = true;
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.GiaTriThe].Visible = true;
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.MaLoaiTheVip].Visible = true;
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.SoDiemThuong].Visible = true;
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.TenLoaiThe].Visible = true;
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.GhiChu].HeaderText = "Ghi Chú";
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.GiaTriThe].HeaderText = "Giá Trị Thẻ";
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.MaLoaiTheVip].HeaderText = "Mã Loại Thẻ Vip";
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.SoDiemThuong].HeaderText = "Số Điểm Thưởng";
            dgvHienThi.Columns[Common.Constants.LoaiTheVip.TenLoaiThe].HeaderText = "Tên Loại Thẻ";
            dgvHienThi.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;
            dgvHienThi.AllowUserToAddRows = false;
            dgvHienThi.AllowUserToDeleteRows = false;
            dgvHienThi.AllowUserToResizeRows = false;
            dgvHienThi.RowHeadersVisible = false;
        }
        #endregion

        #region Khởi tạo
        bool checkClose = false;
        Entities.LoaiTheVip loaithevip;
        Entities.LoaiTheVip[] hienthi;

        public frmQuanLy()
        {
            InitializeComponent();
        }
        #endregion

        #region Check Permission
        public bool CheckPer(int hanhDong)
        {
            return Common.Utilities.Check(Common.FormName.QuanLyLoaiTheVip, hanhDong);
        }
        #endregion

        #region Thêm
        private void Them()
        {
            try
            {
                GUI.LoaiTheVip.frmXuLy frm = new GUI.LoaiTheVip.frmXuLy();
                frm.GetData = new GUI.LoaiTheVip.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Sửa
        private void Sua()
        {
            try
            {
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                loaithevip = ((Entities.LoaiTheVip[])dgvHienThi.DataSource)[i];
                if (loaithevip == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                GUI.LoaiTheVip.frmXuLy frm = new GUI.LoaiTheVip.frmXuLy(loaithevip);
                frm.GetData = new GUI.LoaiTheVip.frmXuLy.GetString(GetValue);
                
                frm.Show();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Xóa
        private void Xoa()
        {
            try
            {
                // check per
                if (!CheckPer(3))
                {
                    MessageBox.Show(Common.MSG.MSG11(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                if (i < 0)
                {
                    return;
                }
                if (dgvHienThi.RowCount == 0)
                {
                    return;
                }
                loaithevip = ((Entities.LoaiTheVip[])dgvHienThi.DataSource)[i];
                // check object
                if (loaithevip == null)
                {
                    MessageBox.Show(Common.MSG.MSG20(), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check conflic delete
                if (!CheckConflicDelete())
                {
                    MessageBox.Show(Common.MSG.MSG3(loaithevip.MaLoaiTheVip), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                // check mess
                if (MessageBox.Show(Common.MSG.MSG1(loaithevip.MaLoaiTheVip), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.No)
                {
                    return;
                }
                // check delete
                loaithevip.TaiKhoan = UtilitiesVariable.User;
                if (Common.StaticVariable.InterfaceObject.DeleteLoaiTheVip(loaithevip) == 0)
                {
                    MessageBox.Show(Common.MSG.MSG16(loaithevip.MaLoaiTheVip), Common.MSG.TieuDe, MessageBoxButtons.OK);
                    return;
                }
                HienThi();
            }
            catch (Exception)
            { }
        }
        #endregion

        #region Check Conflic Delete
        public bool CheckConflicDelete()
        {
            try
            {
                Entities.LoaiTheVip[] temp = Common.StaticVariable.InterfaceObject.SelectLoaiTheVip("1");
                for (int i = 0; i < temp.Length; i++)
                {
                    if (loaithevip.MaLoaiTheVip == temp[i].MaLoaiTheVip)
                    {
                        return true;
                    }
                }
                return false;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Tìm
        public void TimKiem()
        {
            if (rdoMa.Checked)
                dgvHienThi.DataSource = (Entities.LoaiTheVip[])hienthi.Where(p => p.MaLoaiTheVip.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            else
                dgvHienThi.DataSource = (Entities.LoaiTheVip[])hienthi.Where(p => p.TenLoaiThe.ToUpper().IndexOf(txtTimKiem.Text.ToUpper()) >= 0).ToArray();
            fix();
        }
        #endregion

        #region Thoát
        private void Thoat()
        {
            this.Close();
        }
        #endregion

        #region Event
        private void frmQuanLy_Load(object sender, EventArgs e)
        {
            lblTenNhanVien.Text = UtilitiesVariable.User.TenDangNhap;
            try
            {
                dgvHienThi.CellClick += new DataGridViewCellEventHandler(dgvHienThi_CellClick);
                dgvHienThi.CellDoubleClick += new DataGridViewCellEventHandler(dgvHienThi_CellDoubleClick);
                dgvHienThi.KeyDown +=new KeyEventHandler(dgvHienThi_KeyDown);
                btnSua.Click += new EventHandler(btnSua_Click);
                btnThem.Click += new EventHandler(btnThem_Click);
                btnThoat.Click += new EventHandler(btnThoat_Click);
                btnXoa.Click += new EventHandler(btnXoa_Click);
                btnTim.Click += new EventHandler(btnTim_Click);
                txtTimKiem.TextChanged += new EventHandler(txtTimKiem_TextChanged);
                this.FormClosing +=new FormClosingEventHandler(frmQuanLy_FormClosing);
                HienThi(); this.WindowState = FormWindowState.Maximized;
            }
            catch
            {
            }
        }

        void dgvHienThi_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                Sua();
            }
        }

        void txtTimKiem_TextChanged(object sender, EventArgs e)
        {
            TimKiem();
        }

        void dgvHienThi_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            Sua();
        }
        int i = 0;
        void dgvHienThi_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            i = e.RowIndex;
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            Them();
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            Sua();
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            Xoa();
        }

        void btnTim_Click(object sender, EventArgs e)
        {
            TimKiem();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            Thoat();
        }

        private void frmQuanLy_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!checkClose)
                if (MessageBox.Show(Common.MSG.MSG2(), Common.MSG.TieuDe, MessageBoxButtons.YesNo) == System.Windows.Forms.DialogResult.Yes)
                    e.Cancel = checkClose;
                else
                    e.Cancel = !checkClose;
        }
        #endregion

    }
}
#endregion